<?php
namespace Monkey;

use Monkey\Cache;

/**
 * 缓存管理器\Monkey\CacheManager
 * @package    Monkey
 * @author     HuangYi
 * @copyright  Copyright (c) 2011-07-01——2013-03-30
 * @license    New BSD License
 * @version    $Id: \Monkey\CacheManager.php 版本号 2013-03-30 $
 */
final class CacheManager {
    private static $cacher=array();
    /**
     * @param string|null $type 默认由配置指定
     * @return \Monkey\Cache\_Interface
     */
    public static function getCacheByType($type=''){
        static $ini;
        !$ini and $ini=oApp()->cacheSet;
        $type==='' and $type=$ini['type'];
        //$type=ucfirst(strtolower($type));//
        switch($type){
            case 'Apc':return self::getApcCache();
            case 'Memcache':{
                !self::$cacher['Memcache'] and self::$cacher['Memcache']=self::getMemcacheCache($ini);
                return self::$cacher['Memcache'];
            }
            case 'Eaccelerator':return self::getEacceleratorCache();
            case 'Xcache':return self::getXcache();
            default :{
                !self::$cacher['File'] and self::$cacher['File']=self::getFileCache();
                return self::$cacher['File'];
            }
        }
    }
    /**
     * @return \Monkey\Cache\Apc
     */
    public static function getApcCache(){
        !self::$cacher['Apc'] and self::$cacher['Apc']=new Cache\Apc();
        return self::$cacher['Apc'];
    }
    /**
     * @return \Monkey\Cache\Eaccelerator
     */
    public static function getEacceleratorCache(){
        !self::$cacher['Eaccelerator'] and self::$cacher['Eaccelerator']=new Cache\Eaccelerator();
        return self::$cacher['Eaccelerator'];
    }
    /**
     * @param string|null $filename 文件名，默认为 data
     * @param string|null $dir 文件路径，默认为 TEMP.'/filecache'
     * @param string|null $filesize 文件大小，默认为 '15M'
     * @return \Monkey\Cache\File
     */
    public static function getFileCache($filename=null,$dir=null,$filesize=null ){
        static $config=array();
        !$config and $config=oApp()->cacheSet;
        $filename and $config['filename']=$filename;
        $dir and $config['dir']=$dir;
        $filesize and $config['filesize']=$filesize;
        return new Cache\File($config);
    }
    /**
     * @param array $config
     * @return \Monkey\Cache\Memcache
     */
    public static function getMemcacheCache($config){
        return new Cache\Memcache($config);
    }
    /**
     * @return \Monkey\Cache\Xcache
     */
    public static function getXcache(){
        !self::$cacher['Xcache'] and self::$cacher['Xcache']=new Cache\Xcache();
        return self::$cacher['Xcache'];
    }
}